from unittest import TestCase
from ddt import ddt, data, unpack
from selenium import webdriver
from LoginOperation import LoginOperation
from LoginData import Login_data


@ddt
class TestLogin(TestCase):

    @data(*Login_data.login_success_data)
    @unpack
    def testLogin1(self, casename, name, pwd, expect):
        #  开始测试
        driver = webdriver.Chrome()
        # 打开浏览器页面并最大化
        driver.get("http://localhost:90")
        driver.maximize_window()
        # 执行登录
        login = LoginOperation()
        login.login(driver, name, str(int(pwd)))
        # 获取登陆成功结果
        result = login.getSuccessResult(driver)

        if result != expect:
            driver.save_screenshot(casename + ".jpg")
        else:
            print("通过")

        driver.quit()

        self.assertEquals(expect, result)

    @data(*Login_data.login_name_Error_data)
    @unpack
    def testLogin2(self, casename, name, pwd, expect):

        driver = webdriver.Chrome()

        driver.get("http://localhost:90")
        driver.maximize_window()

        login = LoginOperation()
        login.login(driver, name, str(int(pwd)))

        result = login.getNameErrorResult(driver)

        if result != expect:
            driver.save_screenshot(casename + ".jpg")
        else:
            print("通过")

        driver.quit()

        self.assertEquals(expect, result)

    @data(*Login_data.login_pwd_Error_data)
    @unpack
    def testLogin3(self, casename, name, pwd, expect):

        driver = webdriver.Chrome()

        driver.get("http://localhost:90")
        driver.maximize_window()

        login = LoginOperation()
        login.login(driver, name, str(int(pwd)))

        result = login.getPwdErrorResult(driver)

        if result != expect:
            driver.save_screenshot(casename + ".jpg")
        else:
            print("通过")

        driver.quit()

        self.assertEquals(expect, result)
